//
//  RestaurantInformationAndADView.m
//  XRmenu
//
//  Created by Gao Yan 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "RestaurantInformationAndADView.h"
#import "ADVideoView.h"

@implementation RAndAView
@synthesize adPng;
@synthesize adMp3;
@synthesize adMp4;

-(id)initWithViewName:(CGRect)frame
{
	NSDictionary* aDic = [self findNode:APPSESSION.allconfig withKey:@"AD View"];
	if ((self=[super initWithConfig:aDic])) {

		[self add1fingerLeftSwipe];
		[self add1fingerRightSwipe];
		[self add1fingerTopSwipe];
		[self add1fingerDownSwipe];
		
		self.adPng = @"";
		self.adMp3 = @"";
		self.adMp4 = @"";
		arrPng = [[NSMutableArray alloc] init];
		
		NSString *folder = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:@"ad"];
		NSArray *files = [XRmenuFileManage allFilesInFolder:folder];
	
		NSString *extension;
		for (int i=0; i<[files count]; i++) {
			extension=[[files objectAtIndex:i] pathExtension];
			if ([extension isEqualToString:@"png"]) {
				self.adPng =[NSString stringWithFormat:@"%@/%@",folder,[files objectAtIndex:i]];
			}
			else if ([extension isEqualToString:@"mp3"])
			{
				self.adMp3 =[NSString stringWithFormat:@"%@/%@",folder,[files objectAtIndex:i]];
			}
			else if ([extension isEqualToString:@"mp4"])
			{
				self.adMp4 =[NSString stringWithFormat:@"%@/%@",folder,[files objectAtIndex:i]];
			}
		}
		
		folder = [[XRmenuFileManage documentPath] stringByAppendingPathComponent:@"ad/others"];
		files=[XRmenuFileManage allFilesInFolder:folder];
		for (int i=0; i<[files count]; i++) {
			extension=[[files objectAtIndex:i] pathExtension];
			if ([extension isEqualToString:@"png"]) {
				[arrPng addObject:[NSMutableString stringWithFormat:@"%@/%@",folder,[files objectAtIndex:i]]];
			}
		}
		
		
		btnVideo = (UIButton *)[self viewWithTag:kTagOfStartBT];
		imgView = (UIImageView *)[self viewWithTag:kTagOfStartImgV];
		
		if ([self.adMp4 isEqualToString:@""]) {
			[btnVideo setHidden:YES];
		}
		else {
			[btnVideo setHidden:NO];
		}
		
		ii=0;
		
		if ([self.adPng isEqualToString:@""]) {
			
		}else {
			[arrPng insertObject:self.adPng atIndex:0];
			
		
			UIImage *img = [UIImage imageWithContentsOfFile:[arrPng objectAtIndex:ii]];
			[imgView setImage:img];
			
			
			if (![self.adMp3 isEqualToString:@""]) {
				[APPSESSION playSoundOnEvent:adMp3];
			}
			
//			NSTimer *t = [[NSTimer alloc] initWithFireDate:[[NSDate date] addTimeInterval:5] interval:5 target:self selector:@selector(handleThis:) userInfo:nil repeats:YES];
//			[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
//			self.nowTimer = t;
//			[t release];
//			
			isFirstShow=YES;
			
			timer = [NSTimer timerWithTimeInterval:3
											target:self
										  selector:@selector(onTimer)
										  userInfo:nil
										   repeats:YES];
			
			[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
		}
		
	}
	return self;
}


-(void)onTimer{

	UIView *view = [self viewWithTag:331];
	if (view) {
		return;
	}
	
	if (!isFirstShow) {
		[UIView beginAnimations:@"animationImg" context:nil];
		[UIView setAnimationDuration:0.5f];
		[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
		[UIView setAnimationRepeatAutoreverses:NO];
		
		[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self cache:YES];
		
		[UIView commitAnimations];
	}
	else {
		isFirstShow=NO;
	}

		
	UIImage *img = [UIImage imageWithContentsOfFile:[arrPng objectAtIndex:ii]];
		if (ii==0) {
			if (![self.adMp4 isEqualToString:@""]) {
				[btnVideo setHidden:NO];
			}
		}
		else {
			[btnVideo setHidden:YES];
		}
	
	
		ii++;
		if (ii == [arrPng count]) {
			ii = 0;
		}
		[imgView setImage:img];

		
	
}

#pragma mark 手势操作
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
	[self closeAD];
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
	[self closeAD];
}


- (void)handleSwipeTop:(UISwipeGestureRecognizer *)recognizer
{
	[self closeAD];	
	
}
-(void)handleSwipeDown:(UISwipeGestureRecognizer *)recognizer
{
	[self closeAD];
}

-(void) buttonClicked:(id)sender
{
	NSString *RestaurantName=[[NSUserDefaults standardUserDefaults] objectForKey:@"rest_name_preference"];

	if (RestaurantName) {
	}
	else {
		RestaurantName = @"XRmenu 1.0";
	}

	ADVideoView *videoView = [[ADVideoView alloc] initWithViewName:@"AD Video View" videoName:RestaurantName videoFileName:adMp4];
	videoView.tag=331;
	[self addSubview:videoView];
	[videoView release];
}

-(void)closeAD
{
	[timer invalidate];
	if (![adMp3 isEqualToString:@""]) {
		[APPSESSION  stopSoundOnEvent];
	}
	
	NSDictionary *infos1 = [NSDictionary dictionaryWithObjectsAndKeys:num(ViewOfCommend), @"Step", nil];
	postNWithInfos(kMessage_RootView_ChangeStep, self, infos1);
}

-(void)dealloc
{
	[adPng release];
	[adMp3 release];
	[adMp4 release];
	[arrPng release];
	[super dealloc];
}

@end
